//*****************************************字符串处理类*****************************************
// 清除两边的空格
String.prototype.trim = function() {
  return this.replace(/(^\s*)|(\s*$)/g, '');
};
// 清除两边的小括号
String.prototype.trimc = function() {
	var value = this;
	if (value.charAt(0) === '(') {
		value = value.substr(1, value.length);
	}
	if (value.charAt(value.length-1) === ')') {
		value = value.substr(0, value.length-1);
	}
	return value;
};
// 保留数字
String.prototype.GetNum = function() {
  var regEx = /[^\d]/g;
  return this.replace(regEx, '');
};
// 保留中文
String.prototype.GetCN = function() {
  var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g;
  return this.replace(regEx, '');
};
// RGB颜色转换为16进制
String.prototype.colorHex = function(){
	//十六进制颜色值的正则表达式
	var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
	var that = this;
	if(/^(rgb|RGB)/.test(that)){
		var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",");
		var strHex = "#";
		for(var i=0; i<aColor.length; i++){
			var hex = Number(aColor[i]).toString(16);
			//2018-01-23 by lxg
			if (hex.length < 2) {
				hex = "0" + hex;
			}
			strHex += hex;
		}
		if(strHex.length !== 7){
			strHex = that;	
		}
		return strHex;
	}else if(reg.test(that)){
		var aNum = that.replace(/#/,"").split("");
		if(aNum.length === 6){
			return that;	
		}else if(aNum.length === 3){
			var numHex = "#";
			for(var i=0; i<aNum.length; i+=1){
				numHex += (aNum[i]+aNum[i]);
			}
			return numHex;
		}
	}else{
		return that;	
	}
};
//添加数组排序接口
Array.prototype.sortx = function(isDesc){
	this.sort(function(m,n){return m-n;});
	if (isDesc) {
		this.reverse();
	}
};
Array.prototype.contains = function(value) {
	for (var i in this) {
		if (this[i] == value) {
			return true;
		}
	}
	return false;
};
//*****************************************共通类*****************************************
document.onmousedown = handleEvent;

function handleEvent(e) {
    e = e || window.event;
    var target = e.srcElement || e.currentTarget || e.target;

    if (("undefined" != typeof RxTree) && target.isMenu != true) {
        $('.rxtree').each(function(){
            if ($(this)[0].isMenu == true && $(this)[0].style.display == "block") {
                $(this).css("display", "none");
            }
        });
    }
    if ("undefined" != typeof RxMenu && target.isMenu != true) {
        for (var i = 0; i < RxMenu.menus.length; i++) {
            RxMenu.menus[i].hide();
        }
    }
}

window.isCtrlKey = false;
document.onkeydown = function(e) {
    e = e || window.event;
    
	if (e.keyCode == 17) {
		window.isCtrlKey = true;
	}
};
document.onkeyup = function(e) {
	e = e || window.event;
	if (e.keyCode == 17) {
		window.isCtrlKey = false;
	}
};

function RxCommon() {
}

RxCommon.disableSelection = function(target) {
    if (target) {
        if (typeof target.onselectstart) {
            target.onselectstart = function() {
                return false;
            };
        } else if (typeof target.MozUserSelect) {
            target.MozUserSelect = "none";
        } else {
            target.onmousedown = function() {
                return false;
            };
        }
    }
};

RxCommon.toHtmlEmpty = function(value) {
    if (value == "" || value == null || value == undefined) {
        return "&nbsp;"
    } else {
        return value;
    }
};
RxCommon.isEmpty = function(value) {
    if (value == null || value == undefined) {
        return true;
    } else if (typeof(value) == 'string' && value == "") {
        return true;
    } else {
        return false;
    }
};
RxCommon.isEmptyRxd = function(value) {
    if (value) {
        if (value.isEmpty()) {
            return true;
        }
        return false;
    } else {
        return true;
    }
};

RxCommon.isArray = function(value) {
    return (Object.prototype.toString.call(value) === '[object Array]');
};
RxCommon.isString = function(value) {
    return (Object.prototype.toString.call(value) === '[object String]');
};
RxCommon.isNumber = function(value) {
    return (Object.prototype.toString.call(value) === '[object Number]');
};
RxCommon.isDate = function(value) {
    return (Object.prototype.toString.call(value) === '[object Date]');
};
RxCommon.isFunction = function(value) {
    return (Object.prototype.toString.call(value) === '[object Function]');
};
RxCommon.isObject = function(value) {
    return (Object.prototype.toString.call(value) === '[object Object]');
};
RxCommon.isBoolean = function(value) {
    return (Object.prototype.toString.call(value) === '[object Boolean]');
};
RxCommon.getType = function(value) {
	var dataType = Object.prototype.toString.call(data).substring(8);
	dataType = dataType.substring(0, dataType.length - 1);
    return dataType;
};

RxCommon.toEmpty = function(value) {
    if (RxCommon.isEmpty(value)) {
        return "";
    } else {
        return value;
    }
};
RxCommon.toDefault = function(value, defaultValue) {
    if (RxCommon.isEmpty(value)) {
        return defaultValue;
    } else {
        return value;
    }
};
RxCommon.ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
RxCommon.toColName = function(value) {
    var colName = "";
    var mod = value % 26;
    var n = Math.floor(value/26);
    if (n > 0) {
        colName = RxCommon.ALPHA.charAt(n-1) + RxCommon.ALPHA.charAt(mod);
    } else {
        colName = RxCommon.ALPHA.charAt(mod);
    }
    return colName;
};
RxCommon.toColIndex = function(value) {
    var colIndex = null;
    
    if (value.length == 1) {
        colIndex = RxCommon.ALPHA.indexOf(value);
    } else {
        colIndex = (RxCommon.ALPHA.indexOf(value.charAt(0)) + 1) * 26 + RxCommon.ALPHA.indexOf(value.charAt(1));
    }
    return colIndex;
};
//*****************************************日期处理类*****************************************
// 前补充0
RxCommon.prefixZero = function(value, n) {
    var start = 30 + (""+value).length - n;
    return ("000000000000000000000000000000" + value).substring(start);
};

//*****************************************颜色处理类*****************************************
//将#XXXXXX颜色格式转换为RGB格式，并附加上透明度
RxCommon.crgba = function(hex, opacity) {
    if( ! /#?\d+/g.test(hex) ) return hex; //如果是“red”格式的颜色值，则不转换。//正则错误，参考后面的PS内容
    var h = hex.charAt(0) == "#" ? hex.substring(1):hex,
        r = parseInt(h.substring(0,2),16),
        g = parseInt(h.substring(2,4),16),
        b = parseInt(h.substring(4,6),16),
        a = opacity;
    return "rgba(" + r + "," + g + "," + b + "," + a + ")";
}
//将RGBA格式颜色转换为RGB颜色
RxCommon.RGBA2RGB = function(value, bgColor){
	var arr = value.replace(/(?:\(|\)|rgba|RGBA)*/g,"").split(",");
	var a = arr[3];
	//bgColor为背景颜色。默认为白色背景。白色背景下bgColor = 1;黑色背景下bgColor = 0
	if(RxCommon.isEmpty(bgColor)){
		bgColor = 1;
	}
	var r = bgColor * (1 - a) + arr[0] * a;
	var g = bgColor * (1 - a) + arr[1] * a;
	var b = bgColor * (1 - a) + arr[2] * a;
	return "rgb(" + parseInt(r) + "," + parseInt(g) + "," + parseInt(b) + ")";
}
//将rgba格式颜色转换为十六进制
RxCommon.RGBA2Hex = function(value){
	var arr = value.replace(/(?:\(|\)|rgba|RGBA)*/g,"").split(",");
	var colorHex = "#";
	for ( var i = 0; i < arr.length; i++) {
		var hex = "";
		if (i == arr.length - 1) {
			hex = Number(parseInt(arr[i] * 255)).toString(16);
		} else {
			hex = Number(arr[i]).toString(16);
		}
		if (hex.length == 1) {
			hex = "0" + hex;
		}
		colorHex += hex;
	}
	if (parseInt(arr[arr.length - 1] * 255) === 0) {
		return "";
	}
	return colorHex;
}
//将rgb和rgba格式颜色转换为十六进制颜色(注：形如"#ccc"格式的颜色转换成"#cccccc")
RxCommon.formatColor = function(color){
	
	if (/^#([0-9a-fA-f]{6})$/.test(color)) {
		return color;
	} else if (/^#([0-9a-fA-f]{3})$/.test(color)) {
		var arr = color.replace(/#/,"").split("");
		var colorHex = "#";
		for ( var i = 0; i < arr.length; i++) {
			colorHex += (arr[i] + arr[i]);
		}
		return colorHex;
	} else if (/^(rgba|RGBA)/.test(color)) {
		return RxCommon.RGBA2Hex(color);
	} else if (/^(rgb|RGB)/.test(color)){
		return color.colorHex();
	}
	
	return color;
}
RxCommon.crgb = function(r, g, b) {
    var sr = "00" + r.toString(16);
    var sg = "00" + g.toString(16);
    var sb = "00" + b.toString(16);
    sr = sr.substring(sr.length-2).toUpperCase();
    sg = sg.substring(sg.length-2).toUpperCase();
    sb = sb.substring(sb.length-2).toUpperCase();
    return "#" + sr + sg + sb;
}

//*****************************************控件处理类*****************************************
RxCommon.dispose = function(isAll) {
    while (this.hasChildNodes()) {
        this.removeChild(this.lastChild);
    }
    if (isAll == true) {
        var parentNode = this.parentNode;
        if (parentNode) {
            parentNode.removeChild(this);
        }
    }
};
RxCommon.ce = function(parent, id, tagName, className) {
	var ctrl = document.createElement(tagName);
	ctrl._self = this;
	ctrl._owner = parent;
	if (id) {
		ctrl.id = id;
	}
	if (className) {
		ctrl.className = className;
	}
	if (parent) {
		parent.appendChild(ctrl);
	}
	return ctrl;
};
RxCommon.setStyle = function(ctrl, style) {
	if (!ctrl) {
		return;
	}
    var cssText = "";
    for (var prop in style) {
        ctrl.style[prop] = style[prop];
    }
};
RxCommon.getStyle = function(ctrl, styleName) {
	if (!ctrl) {
		return;
	}
    var style = null;
    if (getComputedStyle) {
        style = getComputedStyle(ctrl,"style")[styleName];
    } else {
        style = getStyle(ctrl,"style")[styleName];
    }
    return style;
};
RxCommon.getEOP = function(ctrl) {
	var point = {x:0, y:0};
	if (navigator.userAgent.indexOf("Firefox") < 0) {
		point.x = point.x + ctrl.offsetLeft + parseInt(RxCommon.getStyle(ctrl, "borderLeftWidth").replace("px",""));
		point.y = point.y + ctrl.offsetTop + parseInt(RxCommon.getStyle(ctrl, "borderTopWidth").replace("px",""));
		var pctrl = ctrl.offsetParent;
		while (pctrl) {
			point.x = point.x + pctrl.offsetLeft + parseInt(RxCommon.getStyle(ctrl, "borderLeftWidth").replace("px",""));
			point.y = point.y + pctrl.offsetTop + parseInt(RxCommon.getStyle(ctrl, "borderTopWidth").replace("px",""));
			pctrl = pctrl.offsetParent;
		};
	} else {
		point.x = ctrl.offsetLeft;
		point.y = ctrl.offsetTop;
	}
	return point;
};
/* 格式化文字 */
RxCommon.format = function(value, dataType, format) {
	if (RxCommon.isEmpty(value) || RxCommon.isEmpty(format)) {
		return value;
	}
	var fmtstr = format.replace(/#/g, "0");
	var ov = (""+value).replace(/,/g, "");;
	if (dataType == "number") {
		ov = parseFloat(ov);
		if (ov.toString() == "NaN") {
			return value;
		}
		if (RxCommon.isEmpty(fmtstr)) {
			fmtstr = "0.00";
		}

		var n = fmtstr.indexOf(".");
		var fmtint = "";
		var fmtdec = "";
		if (n >= 0) {
			fmtint = fmtstr.substring(0, n);
			fmtdec = fmtstr.substring(n+1);
		} else {
			fmtint = fmtstr;
			fmtdec = "";
		}
		var isComma = false;
		n = fmtstr.indexOf(",");
		if (n >= 0) {
			isComma = true;
		}

		var valstr = "" + ov.toFixed(fmtdec.length);
		n = valstr.indexOf(".");
		var valint = "";
		var valdec = "";
		if (n >= 0) {
			valint = valstr.substring(0, n);
			valdec = valstr.substring(n+1);
		} else {
			valint = valstr;
			valdec = "";
		}
		if (valint.length == 0) {
			valint = "0";
		}
		if (valdec.length == 0) {
			valdec = "00000000".substring(0, fmtdec.length);
		} else {
			valdec = (valdec+"00000000").substring(0, fmtdec.length);
		}
		//console.log("fmt(" + fmtint + "*" + fmtdec + ")--->val(" + valint + "*" + valdec + ")");

		var result = "";
		if (isComma) {
			for (i = valint.length-1; i >= 0; i--) {
				result = valint.substring(i, i+1) + result;
				if (((valint.length - i) % 3) == 0) {
					result = (i==0?"":",") + result;
				}
			}
			result = result + (valdec.length == 0 ? "" : ("." + valdec));
		} else {
			result = valint + (valdec.length == 0 ? "" : ("." + valdec));
		}
		//console.log("value(" + value + "),format(" + format + "),result(" + result + ")");
		return result;

	} else if (dataType == "date") {
		
	}

	return ov;
};

/*工具方法：将rxdata以key-value的形式存储在数组中返回*/
RxCommon.toPropsArray = function(rxdata, data, pkey){
	for(var i = 0; i < rxdata.keys.length; i++){
		var prop = rxdata.get(rxdata.keys[i]);
		if(prop === undefined){ 
			continue;
		}
		if(prop instanceof RxData){
			RxCommon.toPropsArray(prop, data, pkey + rxdata.keys[i] + '.');
		}else{
			var key = pkey + rxdata.keys[i];
			data.push({key:key, value:prop});
		}
	}
	return data;
};
/* 获取UUID */
RxCommon.ALPHA_NUMBER = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'];
//返回值可能以数字开头
RxCommon.getUUID = function() {
	var id = "";
	for (var i = 0; i < 16; i++) {
		var n = Math.ceil(Math.random()*35);
		id = id + RxCommon.ALPHA_NUMBER[n];
	}
	return id;
};
//返回值以字母开头
RxCommon.getUUID2 = function() {
	var id = "";
	var m = Math.ceil(Math.random()*25);
	id = id + RxCommon.ALPHA_NUMBER[m];
	for (var i = 0; i < 15; i++) {
		var n = Math.ceil(Math.random()*35);
		id = id + RxCommon.ALPHA_NUMBER[n];
	}
	return id;
};

/* 获取文字尺寸 */
RxCommon.word = null;
RxCommon.getTextSize = function(target, text) {
	if (RxCommon.word == null) {
		RxCommon.word = document.createElement("span");
		RxCommon.word.className = "rxword_span";
		document.body.appendChild(RxCommon.word);
	}
	RxCommon.word.style.width = RxCommon.getStyle(target, "width");
	RxCommon.word.style.fontSize = RxCommon.getStyle(target, "fontSize");
	RxCommon.word.style.fontFamily = RxCommon.getStyle(target, "fontFamily");
	RxCommon.word.style.fontWeight = RxCommon.getStyle(target, "fontWeight");
	RxCommon.word.style.fontStyle = RxCommon.getStyle(target, "fontStyle");
	RxCommon.word.style.textDecoration = RxCommon.getStyle(target, "textDecoration");
	RxCommon.word.innerHTML = text;
	return {width:RxCommon.word.offsetWidth, height:RxCommon.word.offsetHeight};
};
RxCommon.getTextSizeForFont = function(font, text) {
	if (this.word == null) {
		this.word = document.createElement("span");
		this.word.className = "rxword_span";
		document.body.appendChild(this.word);
	}
	this.word.style.fontSize = font.fontSize;
	this.word.style.fontFamily = font.fontName;
	this.word.style.fontWeight = font.getFontWeight();
	this.word.style.fontStyle = font.getFontStyle();
	this.word.style.textDecoration = font.getTextDecoration();
	this.word.innerHTML = text;
	return {
		width:this.word.offsetWidth, 
		height:this.word.offsetHeight
	};
}
RxCommon.getMosuePoint = function(eX, eY, targetW, targetH) {
	var point = {x:eX, y:eY, isFullWidth:false, isFullHeight:false};
	var pageH = document.documentElement.clientHeight;
	var pageW = document.documentElement.clientWidth;
//console.log("pageX,pageY,clientWidth,clientHeight:" + pageX + "," + pageY + "," + pageW + "," + pageH);
//console.log("rect.width,rect.height:" + targetW + "," + targetH);
	if (point.y + targetH > pageH) {
		point.y = point.y - targetH - 1;
		point.y = point.y < 0 ? 0:point.y;
		point.isFullHeight = true;
	} else {
		point.y = point.y + 1;
	}
	if (point.x + targetW > pageW) {
		point.x = point.x - targetW - 1;
		point.x = point.x < 0 ? 0:point.x;
		point.isFullWidth = true;
	} else {
		point.x = point.x + 1;
	}
	return point;
};
RxCommon.getBodyOffset = function(obj) {
	var offset = {top:0, left:0};
	while (obj) {
		if (obj.tagName && obj.tagName.toUpperCase() == "BODY") {
			break;
		}
		//TODO：如果是IE或opera浏览器需要考虑追加边框宽度
		offset.top += obj.offsetTop-obj.scrollTop;
		offset.left += obj.offsetLeft-obj.scrollLeft;
		obj = obj.offsetParent
	}
	return offset;
};
//将对象转换成jsonString
RxCommon.toString = function(param){
	var data = '{';
	if (!RxCommon.isEmpty(param)) {
		var count = 0;
		for(var i in param){
			var prop = param[i];
			/*if (prop == undefined || prop == null) {
				continue;
			}*/
			data += "\"" + i + "\":";
			if (RxCommon.isObject(prop)) {
				data += RxCommon.toString(prop);
			} else if (RxCommon.isArray(prop)) {
				data += RxCommon._toArray(prop);
			} else if(RxCommon.isFunction(prop) ){
				data +=  "\"" + prop.toString().replace(/[\r\n]/g,'') +"\"";
			} else{
				if(RxCommon.isNumber(prop)||RxCommon.isBoolean(prop)||prop == null||typeof(prop)=='undefined'){
					data +=  prop ;
				}else{
					data +=  "\""+prop +"\"";
				}
			}
			if(count!=param.length-1){
				data += ","
			}
		}
		if(data.charAt(data.length -1)==','){
			data = data.substring(0,data.length-1);
		}
	}
	data +="}";
	//data = data.replace(/ /g,"!!!");
	return data;
};
RxCommon._toArray = function(data) {
	var rxdata = '[';
	for (var i = 0; i < data.length; i++) {
		if (RxCommon.isObject(data[i])) {
			rxdata += RxCommon.toString(data[i]);
		} else if (RxCommon.isArray(data[i])) {
			rxdata += RxCommon._toArray(data[i]);;
		} else if(RxCommon.isFunction(data[i])){
			rxdata += "\"" + data[i].toString() +"\"";
		}else{
			if(RxCommon.isNumber(data[i])||RxCommon.isBoolean(data[i])||data[i] == null||typeof(data[i]) == 'undefined'){
				rxdata +=  data[i] ;
			}else{
				rxdata +=  "\"" + data[i] +"\"";
			}
		}
		if(i != data.length-1){
			rxdata += ",";
		}
	}
	rxdata += "]";
	return rxdata;
};
/* 算法：获取区域内有效最大空白矩形区域 */
function RxArea(area, rects) {
	this.area = area;
	this.rects = rects;
	this.init();
}
RxArea.prototype = {
	init: function() {
		this.areae = [];
		this.vsize = 20;
		for (var i = 0; i < this.rects.length; i++) {
			var areas = this.getEmptyArea(this.area, this.rects[i]);
			if (i == 0) {
				this.areae = areas;
			} else {
				this.areae = this.getForkArea(this.areae, areas);
			}
		}
		//console.log(this.areae);
	},
	getArea: function() {
		return this.areaa;
	},
	getMaxArea: function(area) {
		var areame = {x:-1,y:-1,w:0,h:0};
		var areamv = {x:-1,y:-1,w:0,h:0};
		for (var i = 0; i < this.areae.length; i++) {
			var rect = this.areae[i];
			if (areame.w < rect.w && areame.h < rect.h) {
				areame = {x:rect.x,y:rect.y,w:rect.w,h:rect.h};
			}
			if (area && area.w < rect.w && area.h < rect.h) {
				if (areamv.w < rect.w && areamv.h < rect.h) {
					areamv = {x:rect.x,y:rect.y,w:rect.w,h:rect.h};
				}
			}
		}
		if (area) {
			if (areamv.x == -1) {
				return areame;
			} else {
				return areamv;
			}
		} else {
			areame;
		}
	},
	getEmptyArea: function(area, rect) {
		var areas = [];
		areas[areas.length] = {x:area.x, y:area.y, w:area.w, h:(rect.y-area.y)};//上
		areas[areas.length] = {x:area.x, y:(rect.y+rect.h), w:area.w, h:(area.y+area.h-rect.y-rect.h)};//下
		areas[areas.length] = {x:area.x, y:area.y, w:(rect.x-area.x), h:area.h};//左
		areas[areas.length] = {x:(rect.x+rect.w), y:area.y, w:(area.x+area.w-rect.x-rect.w), h:area.h};//右
		return areas;
	},
	getForkArea: function(areae, areas) {
		var result = [];
		for (var i = 0; i < areae.length; i++) {
			for (var j = 0; j < areas.length; j++) {
				var area = this.getForkEmptyArea(areae[i], areas[j]);
				if (area != null) {
					result[result.length] = area;
				}
			}
		}
		return result;
	},
	getForkEmptyArea: function(area1, area2) {
		var result = {};
		if (area1.x >= area2.x+area2.w || area1.x+area1.w <= area2.x
		 || area1.y >= area2.y+area2.h || area1.y+area1.h <= area2.y) {
			return null;
		} else {
			if (area1.x <= area2.x) {
				result.x = area2.x;
				if (area1.x+area1.w <= area2.x+area2.w) {
					result.w = area1.x+area1.w-area2.x;
				} else {
					result.w = area2.w;
				}
			} else if (area1.x >= area2.x) {
				result.x = area1.x;
				if (area1.x+area1.w >= area2.x+area2.w) {
					result.w = area2.x+area2.w-area1.x;
				} else {
					result.w = area1.w;
				}
			}
			if (area1.y <= area2.y) {
				result.y = area2.y;
				if (area1.y+area1.h <= area2.y+area2.h) {
					result.h = area1.y+area1.h-area2.y;
				} else {
					result.h = area2.h;
				}
			} else if (area1.y >= area2.y) {
				result.y = area1.y;
				if (area1.y+area1.h >= area2.y+area2.h) {
					result.h = area2.y+area2.h-area1.y;
				} else {
					result.h = area1.h;
				}
			}
		}
		if (result.w <= this.vsize || result.h <= this.vsize) {
			return null;
		} else {
			return result;
		}
	}
}

/* 页面共通方法：与服务器请求等交互接口类 */
function Rx() {
	this.init();
}
Rx.prototype = {
	init: function() {
		this.url = location.href;
		var pos  = this.url.indexOf("?");
		if (pos >= 0) {
			this.page = this.url.substr(0, pos);
			this.param = this.url.substr(pos + 1);
		} else {
			this.page = this.url;
			this.param = null;
		}

		this.urls = [];
		var paths = this.page.split("/");
		this.urls[0] = paths.slice(0,2).join("//"); //协议
		this.urls[1] = paths[2];                    //IP及端口或磁盘
		this.urls[2] = paths.slice(3, 5).join("/"); //模块
		this.urls[3] = paths[5];					//路径
		this.urls[4] = paths[paths.length-1];       //页面

		this.appRoot = "/" + this.urls[2] + "/";
		this.codeBase = "/" + this.urls[2] + "/";//模块路径
		this.httpAppRoot = this.urls.slice(0, 2) + "/";
		this.httpCodeBase =  this.urls.slice(0, 3).join("/") + "/";
	},
	invoke: function(request) {
		var option = this.getOption(request);
		this.ajax.invoke(option);
	},
	getOption: function(request) {
		var options = {
			url: appPath + '/' + request.url,
			data: JSON.stringify(RxCommon.toDefault(request.data, {})),
			type: RxCommon.toDefault(request.type, "post"),
			dataType: RxCommon.toDefault(request.dataType, "json"),
			contentType: RxCommon.toDefault(request.contentType, "application/json; charset=utf-8"),
			async: RxCommon.toDefault(request.async, true),
			error: request.error,
			success: request.success
		};
		return options;
	},
	ajax: {
		axNames:["MSXML2.XMLHTTP.7.0","MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"],
		getRequest:function() {
			var request = null;
			if (window.XMLHttpRequest) {
				request = new XMLHttpRequest();
				if (request.overrideMimeType) {
					request.overrideMimeType("text/json");
				}
			}else if (window.ActiveXObject) {
				for(var i = 0; i < this.axNames.length; i++){
					try {
						request = new ActiveXObject(this.axNames[i]);
						break;
					}catch (e) {
					}
				}
			}
			if (!request) {
				console.log("can't init XMLHttpRequest!");
			}
			return request;
		},
		invoke:function(options) {
			var request = this.getRequest();
			if (!options.contentType) {
				options.contentType = "application/x-www-form-urlencoded"
			}
			if (!options.type) {
				options.type = "post";
			}
			request.onreadystatechange = function() {
				if (request.readyState != 4) {
					return;
				}
				var response = {};
				var errmsg = "";
				if (request.status == 200) {
					var xml = request.responseXML;
					if ( xml && xml.documentElement ) {
						response.xml = xml;
					}
					response.text = request.responseText;
					if (options.success) {
						var data = response.text;
						var dataType = RxCommon.toDefault(options.dataType, "");
						if(dataType.toLowerCase() == "json"){
							data = RxCommon.parseJson(data);
						}
						if (options.success) {
							options.success(data);
						}
					}
					response.ok = true;
				}
				if (!response.ok && options.error) {
					options.error({msg:("request error:" + request.status + ":" + request.responseText)});
				}
				//ie6 不能=null
				try{
					request.onreadystatechange = null;
				}catch(e){
					request.onreadystatechange = function(){};
				}
				request = null;
			};
			var url = options.url;
			request.open(options.type, url, options.async);
			if (options.type == "GET") {
				request.send(null);
			}else {
				request.setRequestHeader("Content-Type", options.contentType);
				request.send(options.data);
			}
		}
	}
}

function RxI() {
	this.init();
}
RxI.prototype = {
	init: function() {
		this.url = location.href;
		var pos  = this.url.indexOf("?");
		if (pos >= 0) {
			this.page = this.url.substr(0, pos);
			this.param = this.url.substr(pos + 1);
		} else {
			this.page = this.url;
			this.param = null;
		}

		this.urls = [];
		var paths = this.page.split("/");
		this.urls[0] = paths.slice(0,2).join("//"); //协议
		this.urls[1] = paths[2];                    //IP及端口或磁盘
		this.urls[2] = paths.slice(3, 5).join("/"); //模块
		this.urls[3] = paths[5];					//路径
		this.urls[4] = paths[paths.length-1];       //页面

		this.appRoot = "/" + this.urls[2] + "/";
		this.codeBase = "/" + this.urls[2] + "/";//模块路径
		this.httpAppRoot = this.urls.slice(0, 2) + "/";
		this.httpCodeBase =  this.urls.slice(0, 3).join("/") + "/";
	},
	invoke: function(request) {
		var option = this.getOption(request);
		this.ajax(option);
	},
	getOption: function(request) {
		var options = {
			url: this.appRoot + request.url,
			data: RxCommon.toDefault(request.data, {}),
			type: RxCommon.toDefault(request.type, "post"),
			dataType: RxCommon.toDefault(request.dataType, "json"),
			contentType: RxCommon.toDefault(request.contentType, "application/json; charset=utf-8"),
			async: RxCommon.toDefault(request.async, true),
			error: request.error,
			success: request.success
		};
		return options;
	},
	ajax:function(options) {
		$.ajax({
			url:options.url,
			data:options.data,
			type:options.type,
			dataType:options.dataType,
			//contentType:options.contentType,
			async:options.async,
			error:options.error,
			success:options.success
		});
	}
}

RxCommon.parseJson = function(json) {
	if (RxCommon.isString(json)) {
		try{
			return JSON.parse(json);
		} catch(e) {
			console.log("parse json failed!"); 
		}
	}
	return json;
}

RxCommon.equals = function(src, des) {
	if (src === des) {
		return true;
	}
	if (src === undefined || src === null || des === undefined || des === null) {
		return false;
	}
	if (RxCommon.isObject(src) && RxCommon.isObject(des)) {
		var rxsrc = new RxData(src);
		var rxdes = new RxData(des);
		return rxsrc.equals(rxdes);
	} else if (RxCommon.isArray(src) && RxCommon.isArray(des) && src.length == des.length) {
		for (var i = 0; i < src.length; i++) {
			if (RxCommon.equals(src[i], des[i]) === false) {
				return false;
			}
		}
		return true;
	}
	return false;
};
//将任意字符串转换为8位定长字符串，且内容相同的字符串转换后结果仍然相同
RxCommon.getAliasName = function (name) {
    var chr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (name.length > 30) {
        var result = name.substring(name.length-30, name.length);
        for (var i = 0; i < name.length-30; i++) {
            var value = (result.charAt(i).charCodeAt() + name.charAt(i).charCodeAt()) % 26;
            result = result.substring(0, i) + chr.charAt(value) + result.substring(i+1, result.length);
        }
        return result;
    } else {
        return name;
    }
};
RxCommon.isInteger = function(value) {
	var nvalue = parseFloat(value);
	if (isNaN(value)) {
	    return false;
	} else {
		return (nvalue %1 === 0);
	}
};
//根据给定值向上取极限值，定义数值域时使用
RxCommon.getCeil = function(value) {
	if (isNaN(value)) return null;
	
	var ceil = 0;
	if (value >= 0) {
		var ratio = Math.pow(10, (value.toFixed(0).length - 1));
		ceil = (Math.ceil(value / ratio)) * ratio;
	} else {
		var ratio = Math.pow(10, (value.toFixed(0).length - 2));
		ceil = (Math.ceil(value / ratio)) * ratio;
	}
	
	return ceil;
};
//根据给定值向下取极限值，定义数值域时使用
RxCommon.getFloor = function(value) {
if (isNaN(value)) return null;
	
	var floor = 0;
	if (value >= 0) {
		var ratio = Math.pow(10, (value.toFixed(0).length - 1));
		floor = (Math.floor(value / ratio)) * ratio;
	} else {
		var ratio = Math.pow(10, (value.toFixed(0).length - 2));
		floor = (Math.floor(value / ratio)) * ratio;
	}
	
	return floor;
};
/**获取日期字符串（YYYY-MM-DD）*/
RxCommon.getDateStr = function(date) {
	if (!RxCommon.isDate(date)) {
		return null;
	}
	
	var dateStr = "";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	
	dateStr += year;
	
	if (month < 10) {
		dateStr += '-' + '0' + month;
	} else {
		dateStr += '-' + month;
	}
	
	if (day < 10) {
		dateStr += '-' + '0' + day;
	} else {
		dateStr += '-' + day;
	}
	
	return dateStr;
};
/**获取时间字符串（HH:mm:ss）*/
RxCommon.getTimeStr = function(date) {
	if (!RxCommon.isDate(date)) {
		return null;
	}
	var h = date.getHours();
	var m = date.getMinutes();
	var s = date.getSeconds();
	
	var timeStr = '';
	if (h < 10) {
		timeStr += '0' + h + ':';
	} else {
		timeStr += h + ':';
	}
	if (m < 10) {
		timeStr += '0' + m + ':';
	} else {
		timeStr += m + ':';
	}
	if (s < 10) {
		timeStr += '0' + s;
	} else {
		timeStr += s;
	}
	
	return timeStr;
};

function initCommonEvent(id) {
	$(".gopt_edit").each(function(){
		var input = $(this).find(".gopt_edit_input")[0];
		var tip  = $(this).find(".gopt_edit_tip")[0];
		input.onblur = function() {
			if (this.value == "") {
				$(tip).show();
			}
		};
		tip.onclick = function() {
			$(this).hide();
			input.focus();
		};
		var self = $(this)[0];
		self.setError = function(msg) {
			$(this).addClass("gopt_edit_error");
			$(tip).addClass("gopt_edit_tip_error");
			$(tip).show();
			self.tips = $(tip).text();
			$(tip).text(msg);
		};
		self.clearError = function() {
			$(this).removeClass("gopt_edit_error");
			$(tip).removeClass("gopt_edit_tip_error");
			$(tip).text(self.tips);
		};
		self.getValue = function() {
			return $(input)[0].value;
		};
		self.setValue = function(value) {
			$(this).removeClass("gopt_edit_error");
			$(tip).removeClass("gopt_edit_tip_error");
			if (RxCommon.isEmpty(value)) {
				$(tip).show();
			}
			$(input)[0].value = value;
		};
	});
}
var msgbox = null;
function showConfirmMsgBox(text, callback) {
	var msg = "确定要删除吗？";
	if (!RxCommon.isEmpty(text)) {
		msg = text;
	}
	var panel = $('#viewPanel')[0];
	msgbox = RxCommon.ce(panel, null, "div", "msgbox");
	msgbox.dispose = RxCommon.dispose;
	var msgboxPanel = RxCommon.ce(msgbox, null, "div", "msgbox_panel");

	var icon = RxCommon.ce(msgboxPanel, null, "div", "msgbox_icon");
	var text = RxCommon.ce(msgboxPanel, null, "div", "msgbox_text");
	text.innerHTML = msg;

	var btnCancel = RxCommon.ce(msgbox, null, "div", "gopt_btn_cancel");
	$(btnCancel).css({left:"15px",bottom:"15px",width:"190px"});
	$(btnCancel).text("取消");
	var btnOk = RxCommon.ce(msgbox, null, "div", "gopt_btn");
	$(btnOk).css({right:"15px",bottom:"15px",width:"190px"});
	$(btnOk).text("确定");

	btnOk.self = msgbox;
	btnOk.onclick = function() {
		msgbox.dispose(true);
		msgbox = null;
		if (callback) {
			callback("ok");
		}
	};

	btnCancel.self = msgbox;
	btnCancel.onclick = function() {
		msgbox.dispose(true);
		msgbox = null;
		if (callback) {
			callback("cancel");
		}
	};
	resizeMsgBox();
};
function showMsgBox(text, callback) {
	var msg = "操作成功？";
	if (!RxCommon.isEmpty(text)) {
		msg = text;
	}
	var panel = $('#viewPanel')[0];
	msgbox = RxCommon.ce(panel, null, "div", "msgbox");
	msgbox.dispose = RxCommon.dispose;
	var msgboxPanel = RxCommon.ce(msgbox, null, "div", "msgbox_panel");

	var icon = RxCommon.ce(msgboxPanel, null, "div", "msgbox_icon");
	var text = RxCommon.ce(msgboxPanel, null, "div", "msgbox_text");
	text.innerHTML = msg;

	var btnOk = RxCommon.ce(msgbox, null, "div", "gopt_btn");
	$(btnOk).css({right:"15px",bottom:"15px",width:"390px"});
	$(btnOk).text("确定");

	btnOk.self = msgbox;
	btnOk.onclick = function() {
		msgbox.dispose(true);
		msgbox = null;
		if (callback) {
			callback("ok");
		}
	};

	resizeMsgBox();
};
function resizeMsgBox() {
	var panel = $('#viewPanel')[0];
	var panelWidth = $('#viewPanel').width();
	var panelHeight = $('#viewPanel').height();
	if (msgbox) {
		msgbox.style.top = ((panelHeight - 250)/2) + "px";
		msgbox.style.left = ((panelWidth - 420)/2) + "px";
	}
}
//站内图保存提示
function SaveConfirmMsgBox(text, callback) {
	var msg = "确定保存接线图吗？";
	if (!RxCommon.isEmpty(text)) {
		msg = text;
	}
	var panel = $('#bodyDiv')[0];
	msgbox = RxCommon.ce(panel, null, "div", "msgbox");
	msgbox.dispose = RxCommon.dispose;
	var msgboxPanel = RxCommon.ce(msgbox, null, "div", "msgbox_panel");

	var icon = RxCommon.ce(msgboxPanel, null, "div", "msgbox_icon");
	var text = RxCommon.ce(msgboxPanel, null, "div", "msgbox_text");
	text.innerHTML = msg;

	var btnCancel = RxCommon.ce(msgbox, null, "div", "gopt_btn_cancel");
	$(btnCancel).css({left:"15px",bottom:"15px",width:"190px"});
	$(btnCancel).text("取消");
	var btnOk = RxCommon.ce(msgbox, null, "div", "gopt_btn");
	$(btnOk).css({right:"15px",bottom:"15px",width:"190px"});
	$(btnOk).text("确定");

	btnOk.self = msgbox;
	btnOk.onclick = function() {
		msgbox.dispose(true);
		msgbox = null;
		if (callback) {
			callback("ok");
		}
	};

	btnCancel.self = msgbox;
	btnCancel.onclick = function() {
		msgbox.dispose(true);
		msgbox = null;
		if (callback) {
			callback("cancel");
		}
	};
	
	sessionStorage.setItem("pageType_", 'route'); 
	resizeMsgBox();
};